<?php
/**
 * Created by jack.
 * User: liangbo
 * Date: 2017/9/23
 * Time: 下午10:42
 * warning:此代码属于内部版权，未经许可，不得转让
 */

namespace backend\controllers;


use yii\web\Controller;
use yii;
use backend\services\ToolService;
use backend\services\UserService;
use Jack\WeChat\Component\Console;
use Jack\WeChat\Component\Utils;
use Jack\WeChat\Core\Robot;
use Jack\WeChat\Message\Formatter\Message;
use backend\services\WeChatFansService;

class ToolsController extends Controller
{
    public $layout = "lte_main";

    /**
     *@功能:邮箱认证
     * @author jack
     *@日期时间:2017/9/23
     */
    public function actionEmailnotice()
    {
        if (Yii::$app->user->identity->email_pass === 0) {
            $uname = yii::$app->user->identity->uname;
            return $this->render("emailnotice", ["uname" => $uname]);

        } else {

            $this->redirect("/site/index");

        }

    }

    /**发送邮件
     *@功能:
     * @author 梁波
     *@日期时间:2017/9/23
     */
    public function actionGetmailcode()
    {
        $toolservice = new ToolService();
        $email = yii::$app->request->post('email', "");
        if (isEmail($email)) {
            $result = $toolservice->sendMessage($email);
            if ($result > 0)
                $res = array("code" => 1, "messages" => "邮件发送成功");
            else
                $res = array("code" => -1, "messages" => "邮件发送失败");
        } else {
            $res = array("code" => -1, "messages" => "邮箱不正确");
        }
        echo json_encode($res);

    }

    /**
     *@功能:验证码校验
     * @author 梁波
     *@日期时间:2017/9/24
     */
    public function actionYzmailcode()
    {
        $usersercie = new UserService();
        $data = yii::$app->request->post('data', "");
        if (!empty($data)) {
            $data = json_decode($data);
            $code = yii::$app->cache->get($data->email);
            if ($code == $data->emailcode) {
                $rel = $usersercie->MailPass($data->email);
                if ($rel)
                    $res = array("code" => 1, "messages" => "验证通过", "url" => "/site/index");
                else
                    $res = array("code" => -1, "messages" => "验证不通过");

            } else
                $res = array("code" => -1, "messages" => "验证码不正确");
        } else
            $res = array("code" => -1, "messages" => "信息不正确");

        echo json_encode($res);
    }

    public function actionQrcode()
    {

        $toolservice = new ToolService();

        $toolservice->on("hello world", function ($res) {

            echo $res;
        });

    }

    /**
     * 改变死粉状态的开发
     */
    public function actionChangefans()
    {
        $uuid = yii::$app->request->get('uuid', "");
        $status = yii::$app->request->get('status', 0);
        WeChatFansService::check_open($uuid, $status);
        echo 200;

    }

}